package logger

import (
	"os"

	"github.com/natefinch/lumberjack"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func LogSetup() {
	infoJack := &lumberjack.Logger{
		Filename:  "./logs/info/log-info.log",
		MaxSize:   1,
		LocalTime: true,
	}
	defer infoJack.Close()

	erroJack := &lumberjack.Logger{
		Filename:  "./logs/erro/log-erro.log",
		MaxSize:   1,
		LocalTime: true,
	}
	defer erroJack.Close()

	config := zap.NewProductionEncoderConfig()
	config.EncodeTime = zapcore.ISO8601TimeEncoder
	config.EncodeCaller = zapcore.ShortCallerEncoder
	fileEncoder := zapcore.NewConsoleEncoder(config)

	// infoFile, _ := os.OpenFile("./log-info.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
	// erroFile, _ := os.OpenFile("./log-erro.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)

	teecore := zapcore.NewTee(
		zapcore.NewCore(fileEncoder, zapcore.AddSync(infoJack), zap.InfoLevel),
		zapcore.NewCore(fileEncoder, zapcore.AddSync(erroJack), zap.ErrorLevel),
		zapcore.NewCore(fileEncoder, zapcore.AddSync(os.Stdout), zap.InfoLevel),
	)

	logger := zap.New(teecore, zap.AddCaller())
	defer logger.Sync()

	zap.ReplaceGlobals(logger)
	zap.L().Info("Logger Setup")
}
